package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.cast.screen.CastScreenManager;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelImportVideoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.ToastUtil;

public class SelImportVideoActivity extends BaseAc<ActivitySelImportVideoBinding> {

    public static int type;//0格式转换 1滤镜 2拼接
    private SelVideoAdapter selVideoAdapter;
    private List<SelectMediaEntity> listShow = new ArrayList<>();
    private int oldPosition;
    private String selPath="";
    private List<String> listPath = new ArrayList<>();
    private String again = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_import_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelImportVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivImport.setOnClickListener(this);
        mDataBinding.rvSelVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selVideoAdapter = new SelVideoAdapter();
        mDataBinding.rvSelVideoList.setAdapter(selVideoAdapter);
        selVideoAdapter.setOnItemClickListener(this);
        //视频拼接再次进入选择
        again = getIntent().getStringExtra(Content.Again);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()){
            case R.id.ivImport:
                if (selPath.isEmpty()&&listPath.size()==0){
                    ToastUtils.showShort(getString(R.string.please_sel_video));
                    return;
                }
                if (type==0){
                    VideoFormatActivity.videoPath=selPath;
                    startActivity(VideoFormatActivity.class);
                }else if (type==1){
                    VideoFilterActivity.videoPath=selPath;
                    startActivity(VideoFilterActivity.class);
                }else if (type==2){
                    if (TextUtils.isEmpty(again)) {
                        VideoSpliceActivity.listPath = listPath;
                        startActivity(VideoSpliceActivity.class);
                    } else {
                        //更换/添加视频
                        Intent intent = new Intent();
                        intent.putExtra(Content.videoPath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }else if (type==3){
                    VideoDecalActivity.videoPath=selPath;
                    startActivity(VideoDecalActivity.class);
                }else if (type==4){
                    VideoSpeedActivity.videoPath=selPath;
                    startActivity(VideoSpeedActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type==2){
            if (TextUtils.isEmpty(again)) {
                if (selVideoAdapter.getItem(position).isChecked()) {
                    selVideoAdapter.getItem(position).setChecked(false);
                    listPath.remove(selVideoAdapter.getItem(position).getPath());
                } else {
                    selVideoAdapter.getItem(position).setChecked(true);
                    listPath.add(selVideoAdapter.getItem(position).getPath());
                }
                selVideoAdapter.notifyItemChanged(position);
            } else {
                selVideoAdapter.getItem(oldPosition).setChecked(false);
                selVideoAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                selVideoAdapter.getItem(position).setChecked(true);
                selVideoAdapter.notifyItemChanged(position);
                selPath = selVideoAdapter.getItem(position).getPath();
            }
        }else {
            selVideoAdapter.getItem(oldPosition).setChecked(false);
            selVideoAdapter.notifyItemChanged(oldPosition);
            oldPosition=position;
            selVideoAdapter.getItem(position).setChecked(true);
            selVideoAdapter.notifyItemChanged(position);
            selPath=selVideoAdapter.getItem(position).getPath();
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_storage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                mDataBinding.tvSelVideoNoData.setVisibility(View.VISIBLE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    listShow.addAll(selectMediaEntities);
                    selVideoAdapter.setList(listShow);
                    mDataBinding.rvSelVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}